﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;
/// <summary>
/// 用作点击道具按钮的功能实现
/// </summary>
public class ButTool : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{

    private bool isClick=false;//是否被点击
    private float dueTime = 0;//开始触碰后累计的时间
    private bool isDisplay;//是否已经显示说明了
    private bool isCdEnd=true;//冷却是否结束
    public  int type;//获取当前的道具是什么
    private Slider cDslider;//获取冷却的slider
    private float cdTime;//冷却时间
    Assets.Scripts.Interface.AProp k;//获取道具自身类
    private ObtainMusic obm;

    public void Start()
    {
        obm = transform.parent.GetComponent<ObtainMusic>();
        cDslider = transform.GetChild(3).GetComponent<Slider>();
        cDslider.gameObject.SetActive(false);
    }
    public void OnPointerDown(PointerEventData eventData)//点击按钮的标记
    {
        isClick = true;
    }
    
    public void OnPointerUp(PointerEventData eventData)//先判断是否显示了提示，没提示就是功能的实现
    {
        if (Time.deltaTime != 0)
        {
            if (!isDisplay&&isCdEnd)
            {
                //调用使用道具功能的函数
                //使用次数减一
                k.Click();
                obm.ChangeClip(obm.musicGather[0]);
                isCdEnd = false;
                cDslider.gameObject.SetActive(true);
                DOTween.To(() =>cDslider.value, x => cDslider.value = x, 0, cdTime).OnComplete(()=> {
                    isCdEnd = true;
                    cDslider.value = 1;
                    cDslider.gameObject.SetActive(false);
                });
            }
            isClick = false;
            isDisplay = false;
        }
    }
    public void Update()//利用unpdate函数来实现点击长按2秒后显示提示，只显示一次
    {
        if (Time.deltaTime != 0)
        {
            k = PropManager.Instance.Items[type];//获取物体的自身脚本
            transform.GetChild(2).GetComponent<Text>().text = k.CountText();//fixme
            cdTime = k.cdTime;
            if (isClick && !isDisplay)
            {
                dueTime += Time.deltaTime;
                if (dueTime > 1)
                {
                    obm.ChangeClip(obm.musicGather[1]);
                    k.Introduce();
                    isDisplay = true;
                }
            }
            else dueTime = 0;
        }
    }


   
}